package com.readbook.util;

/**
 * @(#)com.casic27.platform.util.AssertUtils.java 版权声明 航天光达科技有限公司, 版权所有 违者必究
 *
 * <br> Copyright:： Copyright (c) 2012
 * <br> Company： 航天光达科技有限公司
 * <br> Date：Apr 12, 2012
 * ————————————————————————————————————
 * 修改记录
 * 修改者：
 * 修改时间：
 * 修改原因：
 * —————————————————————————————————————
 */

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 导出
 *
 */
public class ExportExcel {

    public static void download(String modalxls, Map<String, Object> resultm, HttpServletRequest req, HttpServletResponse res) throws ParsePropertyException, IOException, InvalidFormatException {
        String se = File.separator;
        XLSTransformer e = new XLSTransformer();

        String tpl = req.getRealPath(se) + se + "static" + se + modalxls;
        File folder = new File(req.getRealPath(se) + se + "static" + se + modalxls);
        if (!folder.exists()) {
            System.out.print("模板不存在！");
        }

        File excelFolder = new File(req.getRealPath(se) + se + "dload");
        if (!excelFolder.exists()) {
            excelFolder.mkdir();
        }

        long sysid = System.nanoTime();
        String dlname = req.getRealPath(se) + se + "dload" + se + sysid + ".xlsx";
        File excelnow = new File(dlname);
        if (!excelnow.exists()) {
            excelnow.createNewFile();
        }
        if (!excelnow.exists()) {
            System.out.print("导出文件创建失败！");
        }
        e.transformXLS(tpl, resultm, dlname);
        downloadfile(res, req, "dload" + se + sysid + ".xlsx");

    }


    /**
     * @param resp
     * @param fileurl         文件路径
     */
    public static void downloadfile(HttpServletResponse resp, HttpServletRequest request, String fileurl) {
        String se = File.separator;
        String realPath = request.getRealPath(se);
        String path = realPath + se + fileurl;
        File file = new File(path);
        String fileName = file.getName();

        resp.reset();
        resp.setContentType("application/octet-stream");
        resp.setCharacterEncoding("utf-8");
        resp.setContentLength((int) file.length());
        resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {
            os = resp.getOutputStream();
            bis = new BufferedInputStream(new FileInputStream(file));
            int i = 0;
            while ((i = bis.read(buff)) != -1) {
                os.write(buff, 0, i);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static List<Map<String, Object>> downloadExcelfile() throws IOException {
        File srcFile = new File("E:/ccc.xlsx");
        //创建文件字节输入流对象，并接在源上
        InputStream is = new FileInputStream(srcFile);
        XSSFWorkbook wbs = new XSSFWorkbook(is);
        //第一个工作表
        XSSFSheet childSheet = wbs.getSheetAt(0);
        //存储数据
        List<Map<String, Object>> listdata = new ArrayList<>();

        //数据从第一行开始
        for (int j = 0; j <= childSheet.getLastRowNum(); j++) {
            XSSFRow row = childSheet.getRow(j);
            Map<String, Object> data_row = new HashMap<String, Object>();
            StringBuilder row_value = new StringBuilder();

            XSSFCell cell0 = row.getCell(0);

            XSSFCell cell1 = row.getCell(1);
            if (cell0 != null && cell1 != null) {
                int celltype = cell0.getCellType();
                String bh = "";
                if (celltype == 3) {
                    continue;
                } else {
                    cell0.setCellType(cell0.CELL_TYPE_STRING);
                    bh = ExcelUtils.getXSSFCellValue(cell0);
                }
                data_row.put("bh", bh);

                String name = "";
                celltype = cell1.getCellType();
                if (celltype == 3) {
                    continue;
                } else {
                    cell1.setCellType(cell1.CELL_TYPE_STRING);
                    name = ExcelUtils.getXSSFCellValue(cell1);
                }
                name = name.replaceAll("([1-9]+[0-9]*|0)(\\.[\\d]+)?", "").replaceAll("g", "");

                data_row.put("name", name);

                listdata.add(data_row);
            }
        }
        return listdata;
    }


}
